package com.frejoys.app.controller;

import com.frejoys.common.aop.Authority;
import com.frejoys.common.aop.Authorization;
import com.frejoys.common.constant.CommonConstant;
import com.frejoys.common.dto.admin.AdminVerifyDto;
import com.frejoys.common.dto.app.StoreEnterUpdateStatusDto;
import com.frejoys.common.dto.common.PageDto;
import com.frejoys.common.error.HttpStatus;
import com.frejoys.common.exception.GeneralException;
import com.frejoys.common.response.Result;
import com.frejoys.common.util.CacheKeyUtil;
import com.frejoys.common.util.RequestUtil;
import com.frejoys.common.vo.app.*;
import com.frejoys.dao.entity.User;
import com.frejoys.service.AdminVerifyService;
import com.frejoys.service.AuthRuleService;
import com.frejoys.service.StoreEnterService;
import com.frejoys.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Tag(name = "审核", description = "审核")
@RestController
@RequiredArgsConstructor
@RequestMapping(CommonConstant.routePrefixApp)
@Authorization(Authority.Private)
public class VerifyController {

    private final AdminVerifyService adminVerifyService;

    private final StoreEnterService storeEnterService;

    private final RedissonClient redissonClient;

    private final UserService userService;

    private final AuthRuleService authRuleService;

    @PostMapping("/verify/verifyStoreEnterList")
    @Operation(summary = "代理人查询商家入驻申请列表")
    public Result<List<StoreEnterListVo>> verifyStoreEnterList(@RequestBody @Validated PageDto dto){
        return Result.success(storeEnterService.agentStoreEnterInfo(RequestUtil.userId(),dto));
    }

    @PostMapping("/verify/verifyStoreList")
    @Operation(summary = "代理人查询店铺上架审核列表")
    public Result<List<VerifyStoreListVo>> verifyStoreList(@RequestBody @Validated PageDto dto){
        return Result.success(adminVerifyService.verifyStoreList(RequestUtil.userId(),dto));
    }

    @PostMapping("/verify/verifyGoodsList")
    @Operation(summary = "代理人查询商品审核列表")
    public Result<List<VerifyGoodsListVo>> verifyGoodsList(@RequestBody @Validated PageDto dto){
        return Result.success(adminVerifyService.verifyGoodsList(RequestUtil.userId(),dto));
    }

    @PostMapping("/verify/verifyActivityList")
    @Operation(summary = "代理人查询活动审核列表")
    public Result<List<VerifyActivityListVo>> verifyActivityList(@RequestBody @Validated PageDto dto){
        return Result.success(adminVerifyService.verifyActivityList(RequestUtil.userId(),dto));
    }

    @PostMapping("/verify/verifyList")
    @Operation(summary = "管理员查询审核列表")
    public Result<List<VerifyListVo>> verifyList(@RequestBody @Validated PageDto dto){
        return Result.success(adminVerifyService.verifyList(RequestUtil.userId(),dto));
    }

    @PostMapping("/adminVerify/verify")
    @Operation(summary = "管理员审核操作")
    public Result<String> list(@RequestBody @Validated AdminVerifyDto dto){
        RLock lock = redissonClient.getLock(CacheKeyUtil.adminVerifyLockCacheKey(RequestUtil.userId(), dto.getId()));
        if (lock.tryLock()) {
            try {

                if (dto.getAgentUserId() != null){
                    //校验代理人
                    userService.verifyAgentUserId(dto.getAgentUserId());
                }

                adminVerifyService.adminVerify(dto);


            } finally {
                lock.unlock();
            }
        } else {
            throw new GeneralException(HttpStatus.beingProcessed);
        }
        return Result.success();
    }

    @PostMapping("/verify/agentUpdateStatus")
    @Operation(summary = "代理人审核")
    public Result agentUpdateStatus(@RequestBody @Validated StoreEnterUpdateStatusDto dto){
        RLock lock = redissonClient.getLock(CacheKeyUtil.adminVerifyLockCacheKey(RequestUtil.userId(), dto.getVerifyId()));
        if (lock.tryLock()) {
            try {
                adminVerifyService.agentUpdateStatus(dto);
            } finally {
                lock.unlock();
            }
        } else {
            throw new GeneralException(HttpStatus.beingProcessed);
        }
        return Result.success();
    }

}
